import Vue from 'vue'
import VueRouter from 'vue-router'
import Index from "../views/Index";
import CodeQuery from "../views/CodeQuery";
import SearchInput from "../views/input/SearchInput";
import CodeMapper from "../views/CodeMapper";
import MapperInput from "../views/input/MapperInput";
import Login from "../views/Login";
import Error from "../views/Error";
import TreeView from "../views/TreeView";
import TreeViewInput from "../views/input/TreeViewInput";
import UserManager from "../views/UserManager";
import Warn from "../views/Warn";
import ICD10TreeView from "../views/ICD10TreeView";
import ICD11TreeView from "../views/ICD11TreeView";
import HistoryView from "../views/HistoryView";
import HistoryViewInput from "../views/input/HistoryViewInput";

Vue.use(VueRouter)

const routes = [
    {
      path:'/',
      redirect: '/login'
    },
    {
      path:'/login',
      name:'login',
      component: Login
    },
    {
        path:'/error',
        name: 'error',
        component: Error
    },
    {
        path:'/warning',
        name: 'warning',
        component: Warn
    },
    {
        path: '/IcdManagement',
        component: Index,
        redirect: "/IcdManagement/CodeQuery",
        children: [
            {
                path: '/IcdManagement/TreeView',
                name: '树形展示',
                components: {
                    default: TreeView,
                    inputSlot: TreeViewInput
                },
                children: [{
                    path: '/IcdManagement/TreeView/icd10',
                    name: 'icd10',
                    components: {
                        default:ICD10TreeView
                    }
                },{
                    path: '/IcdManagement/TreeView/icd11',
                    name: 'icd11',
                    components: {
                        default:ICD11TreeView
                    }
                }]
            },
            {
                path: '/IcdManagement/CodeQuery',
                name: '编码管理',
                components: {
                    default: CodeQuery,
                    inputSlot: SearchInput
                }
            },
            {
                path: '/IcdManagement/CodeMapper',
                name: '编码映射',
                components: {
                    default: CodeMapper,
                    inputSlot: MapperInput
                }
            },
            {
                path: '/IcdManagement/History',
                name: '修改历史',
                components: {
                    default: HistoryView,
                    inputSlot: HistoryViewInput
                }
            },
            {
                path: '/IcdManagement/User',
                name: '用户管理',
                components: {
                    default: UserManager
                }
            },
        ]
    },
]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})

router.beforeEach((to,from,next) => {
    if(to.path.startsWith('/login')){
        window.localStorage.removeItem('access-data');
        next();
    } else{
        let account = JSON.parse(window.localStorage.getItem('access-data'));
        if(!account){
            next({path:'/login'});
        } else {
            axios({
                url:'http://127.0.0.1:8081/ICDManagement/user/checkToken',
                method:'get',
                headers:{
                    token:account.token
                }
            }).then((res) => {
                if(!res.data){
                    next({path:'/error'})
                }
                if(to.path.startsWith('/IcdManagement/User')){
                    if(account.role !== 'admin'){
                        next({path:'/warning'})
                    }
                }
            })
            next()
        }
    }
})

export default router
